package drinkkiosk.cards;

import drinkkiosk.Settings;
import drinkkiosk.buttons.EnterButton;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

/**UnlockCard presents Staff with a keypad to unlock the ManagerCard
 * @author Charles Ritchea
 */
public class UnlockCard extends javax.swing.JPanel {
	/**The index of the JLabel to be changed by a NumberButton*/
	private int activeNumberJLabel;
	/**The inputted Number as a String*/
	private String number;
	/**The numerical code needed to unlock ManagerCard*/
	private String passcode;

	/** Creates new form Card */
	public UnlockCard() {
		activeNumberJLabel = 0;
		number = "000";
		passcode = Settings.getKeyCode();
		initComponents();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        SendNumPadJPanel = new javax.swing.JPanel();
        tableNumberButton1 = new drinkkiosk.buttons.NumberButton();
        tableNumberButton2 = new drinkkiosk.buttons.NumberButton(2);
        tableNumberButton3 = new drinkkiosk.buttons.NumberButton(3);
        tableNumberButton4 = new drinkkiosk.buttons.NumberButton(4);
        tableNumberButton5 = new drinkkiosk.buttons.NumberButton(5);
        tableNumberButton6 = new drinkkiosk.buttons.NumberButton(6);
        tableNumberButton7 = new drinkkiosk.buttons.NumberButton(7);
        tableNumberButton8 = new drinkkiosk.buttons.NumberButton(8);
        tableNumberButton9 = new drinkkiosk.buttons.NumberButton(9);
        tableNumberButton0 = new drinkkiosk.buttons.NumberButton(0);
        tableNumberClearButton1 = new drinkkiosk.buttons.ClearButton();
        enterButton1 = new drinkkiosk.buttons.EnterButton();
        numberJLabel0 = new javax.swing.JLabel();
        numberJLabel2 = new javax.swing.JLabel();
        numberJLabel1 = new javax.swing.JLabel();
        enterManagerCodeJLabel = new javax.swing.JLabel();
        welcomeButton1 = new drinkkiosk.buttons.WelcomeButton();
        jLabel1 = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(800, 555));

        SendNumPadJPanel.setOpaque(false);
        SendNumPadJPanel.setLayout(new java.awt.GridLayout(4, 3, 10, 10));
        SendNumPadJPanel.add(tableNumberButton1);
        SendNumPadJPanel.add(tableNumberButton2);
        SendNumPadJPanel.add(tableNumberButton3);
        SendNumPadJPanel.add(tableNumberButton4);
        SendNumPadJPanel.add(tableNumberButton5);
        SendNumPadJPanel.add(tableNumberButton6);
        SendNumPadJPanel.add(tableNumberButton7);
        SendNumPadJPanel.add(tableNumberButton8);
        SendNumPadJPanel.add(tableNumberButton9);
        SendNumPadJPanel.add(tableNumberButton0);
        SendNumPadJPanel.add(tableNumberClearButton1);
        SendNumPadJPanel.add(enterButton1);

        SendNumPadJPanel.setBounds(310, 110, 480, 400);
        jLayeredPane1.add(SendNumPadJPanel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        numberJLabel0.setFont(new java.awt.Font("Tahoma", 0, 48));
        numberJLabel0.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        numberJLabel0.setText("0");
        numberJLabel0.setFocusable(false);
        numberJLabel0.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        numberJLabel0.setOpaque(true);
        numberJLabel0.setBounds(360, 50, 50, 50);
        jLayeredPane1.add(numberJLabel0, javax.swing.JLayeredPane.DEFAULT_LAYER);

        numberJLabel2.setFont(new java.awt.Font("Tahoma", 0, 48));
        numberJLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        numberJLabel2.setText("0");
        numberJLabel2.setFocusable(false);
        numberJLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        numberJLabel2.setOpaque(true);
        numberJLabel2.setBounds(690, 50, 50, 50);
        jLayeredPane1.add(numberJLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        numberJLabel1.setFont(new java.awt.Font("Tahoma", 0, 48));
        numberJLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        numberJLabel1.setText("0");
        numberJLabel1.setFocusable(false);
        numberJLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        numberJLabel1.setOpaque(true);
        numberJLabel1.setBounds(520, 50, 50, 50);
        jLayeredPane1.add(numberJLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        enterManagerCodeJLabel.setBackground(new java.awt.Color(255, 255, 255));
        enterManagerCodeJLabel.setFont(new java.awt.Font("Arial", 0, 36));
        enterManagerCodeJLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        enterManagerCodeJLabel.setText("Enter Manager Code");
        enterManagerCodeJLabel.setOpaque(true);
        enterManagerCodeJLabel.setBounds(330, 10, 450, 40);
        jLayeredPane1.add(enterManagerCodeJLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        welcomeButton1.setBounds(20, 490, 143, 51);
        jLayeredPane1.add(welcomeButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/drinkkiosk/Welcome.png"))); // NOI18N
        jLabel1.setBounds(0, 0, 800, 560);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 800, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 555, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 555, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
 	/**Cycles through the 3 JLabels changes their text to the number of the
	 * NumberButton pressed. Updates number to match the label text.
         * @param digit int the number of the NumberButton pressed
         */
        public void updateTableNumber(int digit){
                switch(activeNumberJLabel){
                        case 0:
                                numberJLabel0.setText(""+digit);
                                number = ""+digit+number.charAt(1)+number.charAt(2); 
                                break;
                        case 1:
                                numberJLabel1.setText(""+digit);
                                number = ""+number.charAt(0)+digit+number.charAt(2); 
                                break;
                        case 2:
                                numberJLabel2.setText(""+digit);
                                number = ""+number.charAt(0)+number.charAt(1)+digit; 
                                break;
                        default:
                                return;
                }
                activeNumberJLabel = ++activeNumberJLabel % 3;
                return;
        }
	/**Sets number and numberJLabels to zero.
	 */
        public void clearTableNumber(){
                number = "000";
                numberJLabel0.setText(""+0);
                numberJLabel1.setText(""+0);
                numberJLabel2.setText(""+0);
        }
	/**When the correct code is entered the CardLayout switches to
	 * ManagerCard
	 * @param evt ActionEvent the event passed from EnterButton
	 */
	public void unlock(ActionEvent evt){
		if(number.equals(passcode)){
		EnterButton button = (EnterButton)evt.getSource();
		JPanel numPanel = (JPanel)button.getParent();
		JLayeredPane layer = (JLayeredPane)numPanel.getParent();
		JPanel card = (JPanel)layer.getParent();
		JPanel cardPanel = (JPanel)card.getParent();
		CardLayout cardLayout = (CardLayout)cardPanel.getLayout();
		cardLayout.show(cardPanel, "manager");
		}
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel SendNumPadJPanel;
    private drinkkiosk.buttons.EnterButton enterButton1;
    private javax.swing.JLabel enterManagerCodeJLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLabel numberJLabel0;
    private javax.swing.JLabel numberJLabel1;
    private javax.swing.JLabel numberJLabel2;
    private drinkkiosk.buttons.NumberButton tableNumberButton0;
    private drinkkiosk.buttons.NumberButton tableNumberButton1;
    private drinkkiosk.buttons.NumberButton tableNumberButton2;
    private drinkkiosk.buttons.NumberButton tableNumberButton3;
    private drinkkiosk.buttons.NumberButton tableNumberButton4;
    private drinkkiosk.buttons.NumberButton tableNumberButton5;
    private drinkkiosk.buttons.NumberButton tableNumberButton6;
    private drinkkiosk.buttons.NumberButton tableNumberButton7;
    private drinkkiosk.buttons.NumberButton tableNumberButton8;
    private drinkkiosk.buttons.NumberButton tableNumberButton9;
    private drinkkiosk.buttons.ClearButton tableNumberClearButton1;
    private drinkkiosk.buttons.WelcomeButton welcomeButton1;
    // End of variables declaration//GEN-END:variables
}
